/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.business.service.impl;

import lombok.extern.slf4j.Slf4j;
import me.zhengjie.business.domain.AdlPc;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.business.repository.AdlPcRepository;
import me.zhengjie.business.service.AdlPcService;
import me.zhengjie.business.service.dto.AdlPcDto;
import me.zhengjie.business.service.dto.AdlPcQueryCriteria;
import me.zhengjie.business.service.mapstruct.AdlPcMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author xiahongbo
* @date 2022-05-25
**/
@Slf4j
@Service
@RequiredArgsConstructor
public class AdlPcServiceImpl implements AdlPcService {

    private final AdlPcRepository adlPcRepository;
    private final AdlPcMapper adlPcMapper;

    @Override
    public Map<String,Object> queryAll(AdlPcQueryCriteria criteria, Pageable pageable){
        Page<AdlPc> page = adlPcRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(adlPcMapper::toDto));
    }

    @Override
    public List<AdlPcDto> queryAll(AdlPcQueryCriteria criteria){
        return adlPcMapper.toDto(adlPcRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public AdlPcDto findById(String bisAdlPcId) {
        AdlPc adlPc = adlPcRepository.findById(bisAdlPcId).orElseGet(AdlPc::new);
        ValidationUtil.isNull(adlPc.getBisAdlPcId(),"AdlPc","bisAdlPcId",bisAdlPcId);
        return adlPcMapper.toDto(adlPc);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AdlPcDto create(AdlPc resources) {
        resources.setBisAdlPcId(IdUtil.simpleUUID()); 
        return adlPcMapper.toDto(adlPcRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AdlPc resources) {
        log.info("AdlPcServiceImpl update resources : {} ", resources);
        AdlPc adlPc = adlPcRepository.findById(resources.getBisAdlPcId()).orElseGet(AdlPc::new);
        log.info("AdlPcServiceImpl update AdlPc : {} ", adlPc);
        ValidationUtil.isNull( adlPc.getBisAdlPcId(),"AdlPc","id",resources.getBisAdlPcId());
        adlPc.copy(resources);
        adlPcRepository.save(adlPc);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String bisAdlPcId : ids) {
            adlPcRepository.deleteById(bisAdlPcId);
        }
    }

    @Override
    public void download(List<AdlPcDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AdlPcDto adlPc : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}